package com.rayo.accesscontrollercopy.module.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.model.db.bean.CurrentUserData;
import com.rayo.accesscontrollercopy.model.db.bean.KeyData;
import com.rayo.accesscontrollercopy.model.db.dao.CurrentUserDao;
import com.rayo.accesscontrollercopy.model.db.dao.CurrentUserImpl;
import com.rayo.accesscontrollercopy.model.db.dao.KeyDataDao;
import com.rayo.accesscontrollercopy.model.db.dao.KeyDataImp;
import com.rayo.accesscontrollercopy.module.adapter.OnClicker;
import com.rayo.accesscontrollercopy.module.adapter.UserAdapter;
import com.rayo.accesscontrollercopy.utils.CancelUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by lyc-pc on 17/7/10.
 */

public class UserActivity extends AppCompatActivity {
    @BindView(R.id.tv_activity_user)
    TextView mTvActivityUser;
    @BindView(R.id.rlv_activity_user)
    RecyclerView mRlvActivityUser;
    private List<CurrentUserData> mList = new ArrayList<>();
    private CurrentUserDao mCurrentUserDao = new CurrentUserImpl();
    private KeyDataDao mKeyDataDao = new KeyDataImp();
    private Subscription mSubscription;
    private UserAdapter mUserAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        ButterKnife.bind(this);
        initData();

    }

    private void initData() {
        mUserAdapter = new UserAdapter(mList, this);
        mRlvActivityUser.setLayoutManager(new GridLayoutManager(this, 2));
        mRlvActivityUser.setAdapter(mUserAdapter);
        mUserAdapter.setOnClicker(new OnClicker() {
            @Override
            public void click(View v, final int pos) {
                CancelUtils.unsubscribe(mSubscription);
                mSubscription = Observable.create(new Observable.OnSubscribe<String>() {
                    @Override
                    public void call(Subscriber<? super String> subscriber) {
                        mCurrentUserDao.delete(mList.get(pos));
                        KeyData data = mKeyDataDao.getKeyDataByTagId(mList.get(pos).getTagid());
                        mKeyDataDao.delete(data);
                        mList.remove(pos);
                        subscriber.onNext("");
                        subscriber.onCompleted();
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {
                        mUserAdapter.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onClick(View v, int pos, KeyData keyData) {

            }

            @Override
            public void longClick(View v, int pos) {

            }
        });
        mSubscription = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                mList.clear();
                mList.addAll(mCurrentUserDao.findAll());
                subscriber.onNext("");
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                mUserAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CancelUtils.unsubscribe(mSubscription);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK){
            setResult(RESULT_OK);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
